package libraryPSI2;



import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.*;

/**
 * Created with IntelliJ IDEA.
 * User: Станислав
 * Date: 10.03.14
 * Time: 19:01
 * To change this template use File | Settings | File Templates.
 */
public class BookManager {
    List<String> toReturn = new LinkedList<String>();
    public List<String> getAllBookfromDB()
    {

        try
        {
            Connection conn = null;
            PreparedStatement ps = null;
            ResultSet rs = null;
            try
            {
                conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/library", "root", "root");
                ps = conn.prepareStatement("Select * from library.author");
                rs = ps.executeQuery();
                String tu = null;
                while (rs.next())
                {
                    tu =(rs.getInt("id")+ rs.getString("name")+ rs.getString("author"));
                    toReturn.add(tu);
                }
            }
            finally
            {
                if (conn != null)
                {
                    conn.close();
                    conn = null;
                }
                if (ps != null)
                {
                    ps.close();
                    ps = null;
                }
                if (rs != null)
                {
                    rs.close();
                    rs = null;
                }
            }
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
        return toReturn;
    }

    public boolean addBookToDB(String book)
    {
        boolean f = false;
        try
        {
            Connection conn = null;
            PreparedStatement ps = null;
            try
            {
                conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/library", "root", "root");
                ps = conn.prepareStatement("Insert into library.books VALUES(?,?,?)");
//                ps.setInt(1, BookAll.getId());
//                ps.setString(2, BookAll.getName());
//                ps.setInt(3, BookAll.getAuthor());

                int res = ps.executeUpdate();
                f = res > 0;
            }
            finally
            {
                if (conn != null)
                {
                    conn.close();
                    conn = null;
                }
                if (ps != null)
                {
                    ps.close();
                    ps = null;
                }
            }
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
        return f;
    }
    }

